﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PingYou.Ado.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            //ExecuteInsertByHelper();
            //ExecuteReaderByHelper();
            AddCourse();
            Console.Read();
        }

        //执行插入方法返回影响到的行数
        static void ExecuteUpdate()
        {
            //创建表
            //            string sql = @"create table Student (
            //            ID int NOT NULL,
            // StudentName NVARCHAR(100),
            // Age int,
            // Gender int ,
            // Remark NVARCHAR(500),
            // Enabled int ,
            // CreateStaff INT,
            // CreateDate datetime,
            // ModifyStaff INT,
            // ModifyDate datetime
            //)
            //alter table Student
            //      add constraint PK_Student_ID primary key(ID); ";

            //Insert操作
            //        string sql = @" insert into Student
            //(ID,StudentName, Age, Gender, Enabled, CreateStaff, CreateDate, ModifyStaff, ModifyDate)
            //values(
            //1,'学生1', 10, 1, 1, 1, getdate(), '', getdate()
            //)";

            string sql = @" insert into Student
            (ID,StudentName, Age, Gender, Enabled, CreateStaff, CreateDate, ModifyStaff, ModifyDate)
            values(
            1,'学生1', 10, 1, 1, 1, getdate(), '', getdate()
            )";
            int result = SQLHelper.ExacuteNoQuery(sql);
            Console.WriteLine("影响条数=" + result);
        }

        //执行插入方法返回插入数据的ID
        static void ExecuteInsertByHelper()
        {
            string sql = @" insert into Student
            (ID,StudentName, Age, Gender, Enabled, CreateStaff, CreateDate, ModifyStaff, ModifyDate)
            values(
            2,'学生2', 10, 1, 1, 1, getdate(), '', getdate()
            )";
            string result = SQLHelper.ExecuteScalar(sql);
            Console.WriteLine("新插入数据的ID=" + result);
        }

        static void ExecuteReaderByHelper()
        {
            string sql = "SELECT ID,StudentName, Age, Gender, Remark FROM Student WHERE Enabled=1";
            SqlDataReader reader = null;
            try
            {
                reader = SQLHelper.ExecuteReader(sql);
                while (reader.Read())
                {
                    Console.WriteLine("ID=" + reader["ID"] + " StudentName=" + reader["StudentName"] + " Age=" + reader["Age"] + " Gender=" + reader["Gender"] + " Remark=" + reader["Remark"]);
                }
            }
            catch (Exception myEx)
            {
                throw myEx;
            }
            finally
            {
                reader.Close();
            }
        }

        static void AddCourse()
        {
            try
            {
                Console.WriteLine("请输入姓名：");
                string paramStudentName = Console.ReadLine();
                int iAage = -1;
                bool IsAgeRight = false;

                do
                {
                    try
                    {
                        Console.WriteLine("请输入年龄：");
                        string paramAge = Console.ReadLine();
                        iAage = Convert.ToInt32(paramAge);
                        IsAgeRight = false;
                    }
                    catch (Exception myEx)
                    {
                        IsAgeRight = true;
                    }
                } while (IsAgeRight);
                int result = new Student().AddStudent(new StudentModel()
                {
                    StudentName = paramStudentName,
                    Age = iAage
                });
                Console.WriteLine("影响条数=" + result);
            }
            catch (Exception myEx)
            {
                throw myEx;
            }
        }
    }
}
